package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.ItemSpecQueryAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.SpecConvertCoefficientDTO;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.SpecConvertCoefficientDetailDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.item.normal.scitem.model.openapi.ResultWrapper;
import com.alibaba.cz.item.normal.scitem.model.openapi.scitem.SpecConvertCoefficientRequest;
import com.alibaba.cz.item.normal.scitem.model.openapi.scitem.SpecConvertCoefficientResponse;
import com.alibaba.cz.item.normal.scitem.openapi.IScItemSpecQueryService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName ItemSpecQueryAdapterImpl
 * @description: TODO
 * @date 2023/06/01 09:54
 * @version: 1.0
 */
@Service
public class ItemSpecQueryAdapterImpl implements ItemSpecQueryAdapter {
    private static final Log log = Log.getLogger(ItemSpecQueryAdapterImpl.class);

    @DubboReference
    private IScItemSpecQueryService iScItemSpecQueryService;

    @Override
    public List<SpecConvertCoefficientDTO> queryConvertCoefficientByMaterialCodeList(List<String> materialCodeList) {
        if(CollectionUtils.isEmpty(materialCodeList)) {
            return Lists.newArrayList();
        }
        SpecConvertCoefficientRequest convertCoefficientRequest = new SpecConvertCoefficientRequest();
        convertCoefficientRequest.setMaterialCodeList(materialCodeList.stream().distinct().collect(Collectors.toList()));
        ResultWrapper<List<SpecConvertCoefficientResponse>> convertCoefficient =
                iScItemSpecQueryService.getConvertCoefficient(convertCoefficientRequest);
        log.info("箱规查询 request={}, result={}", JSONArray.toJSONString(convertCoefficientRequest), JSONObject.toJSONString(convertCoefficient));
        if(Objects.isNull(convertCoefficient) || CollectionUtils.isEmpty(convertCoefficient.getResult())) {
            return Lists.newArrayList();
        }
        return convertSpecConvertCoefficientDTOS(convertCoefficient);
    }

    @Override
    public List<SpecConvertCoefficientDTO> queryConvertCoefficientByProductIdList(List<String> productIdList) {
        if(CollectionUtils.isEmpty(productIdList)) {
            return Lists.newArrayList();
        }
        SpecConvertCoefficientRequest convertCoefficientRequest = new SpecConvertCoefficientRequest();
        convertCoefficientRequest.setProductIdList(productIdList);
        ResultWrapper<List<SpecConvertCoefficientResponse>> convertCoefficient =
                iScItemSpecQueryService.getConvertCoefficient(convertCoefficientRequest);

        if(Objects.isNull(convertCoefficient) || CollectionUtils.isEmpty(convertCoefficient.getResult())) {
            return Lists.newArrayList();
        }
        return convertSpecConvertCoefficientDTOS(convertCoefficient);
    }

    private static List<SpecConvertCoefficientDTO> convertSpecConvertCoefficientDTOS(ResultWrapper<List<SpecConvertCoefficientResponse>> convertCoefficient) {
        return convertCoefficient.getResult().stream().map(p -> {

            List<SpecConvertCoefficientDetailDTO> convertCoefficientDTOS =
                    Optional.ofNullable(p.getConvertCoefficientInfos()).orElse(Lists.newArrayList()).stream()
                            .map(tmp -> {
                                SpecConvertCoefficientDetailDTO detailDTO = new SpecConvertCoefficientDetailDTO();
                                BeanUtils.copyProperties(tmp, detailDTO);
                                return detailDTO;
                            })
                            .collect(Collectors.toList());
            SpecConvertCoefficientDTO specConvertCoefficientDTO = new SpecConvertCoefficientDTO();
            specConvertCoefficientDTO.setMaterialCode(p.getMaterialCode());
            specConvertCoefficientDTO.setProductId(p.getProductId());
            specConvertCoefficientDTO.setOuterId(p.getOuterId());
            specConvertCoefficientDTO.setConvertCoefficientList(convertCoefficientDTOS);
            return specConvertCoefficientDTO;
        }).collect(Collectors.toList());
    }
}
